package cn.fl.kgs.controller;


import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.service.ICacheService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.kgs.utils.AuthenticationUtils;
import cn.fl.kgs.utils.DES;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: 知识图谱
 */
@Controller
@RequestMapping("/knowledgeGraph/init")
public class KnowledgeGraphController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(KnowledgeGraphController.class);

    /**
     * 缓存服务
     */
    @Autowired
    private ICacheService cacheService;

    /**
     * 到图谱规则搜索显示页面
     */
    @RequestMapping(value = "/initGraphSearch.do", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView initGraphSearch(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView("kgs/searchJsp");//加载前端的html 顺便把参数给前端
        String prjId = request.getParameter("prjId");
        String c_no = request.getParameter("c_no");
        String usrId = request.getParameter("usrId");
        BaseLogUtils.info(logger, "initGraphSearch", "加载图谱searchJsp页面.", CurrentThreadContext.getCurrentUserName());

        if(StringUtils.isEmpty(prjId)||StringUtils.isEmpty(c_no)||StringUtils.isEmpty(usrId)){
            view.addObject("errMsg","用户信息不能为空");
            return view;
        }


        if(CurrentThreadContext.getCurrentUserId()!=Long.parseLong(usrId)){
            view.addObject("errMsg","系统用户与操作用户不符！");
            return view;
        }



        String cachestr="kgs:kgshtml:%#@"+prjId+"$!^:(#"+c_no;
        //在缓存里获取
        String token =(String) this.cacheService.get(cachestr, "remote");
        if(null==token){
            token = AuthenticationUtils.reqAuthenticationByappId();//获取token
            BaseLogUtils.info(logger, "getToken", "加载token到searchJsp页面.", CurrentThreadContext.getCurrentUserName());
            this.cacheService.add(cachestr,token,"remote",1800);
        }

        view.addObject("token", DES.encrypt(token));//加密
        view.addObject("prjId", prjId);
        view.addObject("c_no", c_no);
        view.addObject("usrId", usrId);
        return view;
    }


    /**
     * 到图谱规则显示页面
     */
    @RequestMapping(value = "/initGraphVisualPage.do", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView initGraphVisualPage(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView("kgs/visualJsp");//加载前端的html 顺便把参数给前端
        String prjId = request.getParameter("prjId");
        String c_no = request.getParameter("c_no");
        String usrId = request.getParameter("usrId");
        BaseLogUtils.info(logger, "initGraphVisualPage", "加载图谱visualJsp页面.", CurrentThreadContext.getCurrentUserName());

        if(StringUtils.isEmpty(prjId)||StringUtils.isEmpty(c_no)||StringUtils.isEmpty(usrId)){
            view.addObject("errMsg","用户信息不能为空");
            return view;
        }

        if(CurrentThreadContext.getCurrentUserId()!=Long.parseLong(usrId)){
            view.addObject("errMsg","系统用户与操作用户不符！");
            return view;
        }

        String cachestr="kgs:kgshtml:%#@"+prjId+"$!^:(#"+c_no;
        //在缓存里获取
        String token =(String) this.cacheService.get(cachestr, "remote");
        if(null==token){
            token = AuthenticationUtils.reqAuthenticationByappId();//获取token
            BaseLogUtils.info(logger, "getToken", "加载token到visualJsp页面.", CurrentThreadContext.getCurrentUserName());
            this.cacheService.add(cachestr,token,"remote",1800);
        }

        view.addObject("token",  DES.encrypt(token));
        view.addObject("prjId", prjId);
        view.addObject("c_no", c_no);
        view.addObject("usrId", usrId);
        return view;
    }

    /**
     * 到图谱规则组显示页面
     */
    @RequestMapping(value = "/initGraphGroupPage.do", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView initGraphGroupPage(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView("kgs/groupJsp");//加载前端的html 顺便把参数给前端
        String prjId = request.getParameter("prjId");
        String c_no = request.getParameter("c_no");
        String usrId = request.getParameter("usrId");
        String org_code = request.getParameter("org_code");
        BaseLogUtils.info(logger, "initGraphGroupPage", "加载图谱group页面.", CurrentThreadContext.getCurrentUserName());

        if(StringUtils.isEmpty(prjId)||StringUtils.isEmpty(c_no)||StringUtils.isEmpty(usrId)){
            view.addObject("errMsg","用户信息不能为空");
            return view;
        }

        if(CurrentThreadContext.getCurrentUserId()!=Long.parseLong(usrId)){
            view.addObject("errMsg","系统用户与操作用户不符！");
            return view;
        }


        String cachestr="kgs:kgshtml:%#@"+prjId+"$!^:(#"+c_no;
        //在缓存里获取
        String token =(String) this.cacheService.get(cachestr, "remote");
        if(null==token){
            token = AuthenticationUtils.reqAuthenticationByappId();//获取token
            BaseLogUtils.info(logger, "getToken", "加载token到group页面.", CurrentThreadContext.getCurrentUserName());
            this.cacheService.add(cachestr,token,"remote",1800);
        }

        view.addObject("token",  DES.encrypt(token));
        view.addObject("prjId", prjId);
        view.addObject("c_no", c_no);
        view.addObject("usrId", usrId);
        view.addObject("org_code", org_code);
        return view;
    }

    /**
     * 到图谱规则历史显示页面
     */
    @RequestMapping(value = "/initGraphVisualHistoryPage.do", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView initGraphVisualHistoryPage(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView("kgs/visualHistoryJsp");//加载前端的html 顺便把参数给前端
        String prjId = request.getParameter("prjId");
        String c_no = request.getParameter("c_no");
        String usrId = request.getParameter("usrId");
        BaseLogUtils.info(logger, "initGraphVisualHistoryPage", "加载图谱visualHistory页面.", CurrentThreadContext.getCurrentUserName());
        if(StringUtils.isEmpty(prjId)||StringUtils.isEmpty(c_no)||StringUtils.isEmpty(usrId)){
            view.addObject("errMsg","用户信息不能为空");
            return view;
        }

        if(CurrentThreadContext.getCurrentUserId()!=Long.parseLong(usrId)){
            view.addObject("errMsg","系统用户与操作用户不符！");
            return view;
        }

        String cachestr="kgs:kgshtml:%#@"+prjId+"$!^:(#"+c_no; //自定义缓存key
        //在缓存里获取 远程
        String token =(String) this.cacheService.get(cachestr, "remote");
        if(null==token){
            token = AuthenticationUtils.reqAuthenticationByappId();//获取token
            BaseLogUtils.info(logger, "getToken", "加载token到visualHistory页面.", CurrentThreadContext.getCurrentUserName());
            this.cacheService.add(cachestr,token,"remote",1800);//30分钟
        }
        //先把参数扔给jsp 通过iframe方式给到html
        view.addObject("token",   DES.encrypt(token));
        view.addObject("prjId", prjId);
        view.addObject("c_no", c_no);
        view.addObject("usrId", usrId);
        return view;
    }



}

